import React from 'react';
import {inject, observer} from 'mobx-react';
import { WeaErrorPage, WeaDialog, WeaTop, WeaTab, WeaRightMenu, WeaNewScroll, WeaInput, WeaBrowser, WeaTools } from 'ecCom';
import {Row, Button, Spin, Col, Input, Modal} from 'antd';
import {observable, action, toJS} from 'mobx';
import {WeaTableNew} from 'comsMobx';
import RoomCreate from './RoomCreate.js'
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

const WeaTable = WeaTableNew.WeaTable;
const confirm = Modal.confirm;

@inject('meetingRoom')
@observer
class MeetingRoomDialog extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            spinning: true,
        }
    }

    componentDidMount() {

    }
    componentWillReceiveProps(nextProps) {
        const {meetingRoom} = this.props;
        meetingRoom.roomInfoForm.reset();
        if (meetingRoom.viewType == 'create'){
            meetingRoom.getFields();
        } else if (meetingRoom.viewType == 'edit'){
            meetingRoom.getFields({ roomid: nextProps.roomId});
        } else if (meetingRoom.viewType == 'preview' || meetingRoom.viewType == 'share'){
            meetingRoom.getFields({ roomid: nextProps.roomId, prview:1});
        }
        meetingRoom.getRoomShareFields();
    }
    render() {
        const {meetingRoom} = this.props;
        let {spinning, viewType} = meetingRoom;
        let title = viewType == 'create' ? getLabel(383319,"新建会议室") : viewType == 'edit' ? getLabel(383320,"编辑会议室") :  getLabel(383322,"查看会议室");
        return (
            <div className="workPlan-dialog" id="workPlan-createWorkPlan-id">
                <WeaDialog
                    visible={meetingRoom.showDialogFlag}
                    title={title}
                    icon="icon-coms-meeting"
                    iconBgcolor="#0079de"
                    className=""
                    buttons={
                        this.getButton(viewType)
                    }
                    style={{ width: '605px', height: '500px' }}
                    onCancel={this.doClose}
                    >
                    <Spin spinning={spinning} style={{ height: '100%' }}>
                        <RoomCreate ref="roomCreate" viewType ={viewType}/> 
                    </Spin>
                </WeaDialog>
            </div>
        )
    }

    getButton(viewType){
        let btns = [];
        if (viewType == 'create'){
            btns.push(<Button type="primary" onClick={() => this.doSave() }>{getLabel(383323,"保存")}</Button>);
            btns.push(<Button type="ghost" onClick={() => this.doSaveAndEdit() }>{getLabel(32159,"保存并进入详细设置")}</Button>);
        }
        if (viewType == 'edit'){
            btns.push(<Button type="primary" onClick={() => this.doSave() }>{getLabel(383323,"保存")}</Button>);
        }
        btns.push(<Button type="ghost" onClick={() => { this.doClose() } }>{getLabel(130711,"关闭")}</Button>);
        return btns;
    }

    doSaveAndEdit() {
        let {meetingRoom} = this.props;
        let alldatas = meetingRoom.roomInfoForm.getFormParams();
        meetingRoom.doSave(alldatas).then(rdata => {
            meetingRoom.getRoomList(meetingRoom.adSearchForm.getFormParams());
           if(rdata.ret=='true'){
            //    meetingRoom.setRoomId(rdata.id);
            //    meetingRoom.changeViewType('edit');
            //    meetingRoom.showDialog(rdata.id);
           }
        });
    }

    doSave(){
        let {meetingRoom} = this.props;
        let alldatas = meetingRoom.roomInfoForm.getFormParams();
        meetingRoom.doSave(alldatas).then(rdata => {
            meetingRoom.getRoomList(meetingRoom.adSearchForm.getFormParams());
            this.doClose();
        });
    }

    doClose = () => {
        this.props.doClose && this.props.doClose()
    }
}

export default MeetingRoomDialog
